<?php

class logos
{
	var $filename, $filewidth, $filehieght, $stringdim, $selectQ, $updateQ;
	var $existQ, $removeQ, $mode, $image_field, $title = '', $title_field, $cropToHeight, $action, $itemName;
	var $mess = '';
	var $valid = false;
    var $thmbwidth = 0;
    var $thmbheight = 0;
	var $toRender = Array();

	function logos($mode)
	{
		$mess = '';
		$this->valid = false;
		if(!isset($_POST['itemID']))
		{
			$_POST['itemID'] = -1;
		}
		if($mode == 'championships')
		{
			$this->filename = 'champ';
			$this->filewidth = 200;
			$this->fileheight = 150;
                        $this->thmbwidth = 80;
                        $this->thmbheight = 60;
			$this->stringdim = '200x150px';
			$this->selectQ = "select * from ". CHAMPIONSHIPS_TABLE;
			$this->existQ = "select * from ". CHAMPIONSHIPS_TABLE . " Where c_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". CHAMPIONSHIPS_TABLE . " Set c_logoURL = '',  c_logoThmbURL = '' Where c_id = " . $_POST['itemID'];
			$this->updateQ = "update ". CHAMPIONSHIPS_TABLE . " Set c_logoURL = '{logourl}', c_logoThmbURL = '{thmburl}'  Where c_id = " . $_POST['itemID'];
			$this->title_field = 'c_title';
			$this->image_field = 'c_logoURL';
			$this->image_id = 'c_id';
			$this->form_id = 'c_id';
			$this->mode = $mode;
			$this->action = 'championships';
                        $this->cropToHeight = false;
			$this->valid = true;
		}
		else if($mode == 'teams')
		{
			$this->filename = 'team';
			$this->filewidth = 70;
			$this->fileheight = 70;
            $this->thmbwidth = 30;
            $this->thmbheight = 30;
			$this->stringdim = '70x70px';
			$this->selectQ = "select * from ". TEAMS_TABLE;
			$this->existQ = "select * from ". TEAMS_TABLE . " Where t_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". TEAMS_TABLE . " Set t_logo = '', t_logoThmb = '' Where t_id = " . $_POST['itemID'];
			$this->updateQ = "update ". TEAMS_TABLE . " Set t_logo = '{logourl}', t_logoThmb = '{thmburl}' Where t_id = " . 
                                    $_POST['itemID'];
			$this->title_field = 't_name';
			$this->image_field = 't_logo';
			$this->image_id = 't_id';
			$this->form_id = 't_id';
			$this->mode = $mode;
			$this->action = 'team';
            $this->cropToHeight = false;
			$this->valid = true;
		}
		else if($mode == 'divisions')
		{
			$this->filename = 'divisions';
			$this->filewidth = 200;
			$this->fileheight = 150;
            $this->thmbwidth = 80;
            $this->thmbheight = 60;
			$this->stringdim = '200x150px';
			$this->selectQ = "select * from ". DIVISIONS_TABLE . ' ORDER BY d_title ASC';
			$this->existQ = "select * from ". DIVISIONS_TABLE . " Where d_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". DIVISIONS_TABLE . " Set d_logo = '' Where d_id = " . $_POST['itemID'];
			$this->updateQ = "update ". DIVISIONS_TABLE . " Set d_logo = '{logourl}', d_logoThmb = '{thmburl}' Where d_id = " . $_POST['itemID'];
			$this->title_field = 'd_title';
			$this->image_field = 'd_logo';
			$this->image_id = 'd_id';
			$this->form_id = 'd_id';
			$this->mode = $mode;
			$this->action = 'divisions';
            $this->cropToHeight = false;
			$this->valid = true;
		}
		else if($mode == 'tracks')
		{
			$this->filename = 'tracks';
			$this->filewidth = 200;
			$this->fileheight = 150;
            $this->thmbwidth = 80;
            $this->thmbheight = 60;
			$this->stringdim = '200x150px';
			$this->selectQ = "select * from ". TRACKS_TABLE . ' ORDER BY t_fullTitle';
			$this->existQ = "select * from ". TRACKS_TABLE . " Where t_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". TRACKS_TABLE . " Set t_logo = '' Where t_id = " . $_POST['itemID'];
			$this->updateQ = "update ". TRACKS_TABLE . " Set t_logo = '{logourl}', t_logoThmb = '{thmburl}' Where t_id = " . $_POST['itemID'];
			$this->title_field = 't_fullTitle';
			$this->image_field = 't_logo';
			$this->image_id = 't_id';
			$this->form_id = 't_id';
			$this->action = 'tracks';
			$this->mode = $mode;
            $this->cropToHeight = false;
			$this->valid = true;
		}
		else if($mode == 'cars')
		{
			$this->filename = 'cars';
			$this->filewidth = 200;
			$this->fileheight = 150;
            $this->thmbwidth = 80;
            $this->thmbheight = 60;
			$this->stringdim = '200x150px';
			$this->selectQ = "select * from ". CARS_TABLE . ' ORDER BY c_model ASC';
			$this->existQ = "select * from ". CARS_TABLE . " Where c_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". CARS_TABLE . " Set c_logo = '' Where c_id = " . $_POST['itemID'];
			$this->updateQ = "update ". CARS_TABLE . " Set c_logo = '{logourl}', c_logoThmb = '{thmburl}' Where c_id = " . $_POST['itemID'];
			$this->title_field = 'c_model';
			$this->image_field = 'c_logo';
			$this->image_id = 'c_id';
			$this->form_id = 'c_id';
			$this->action = 'cars';
			$this->mode = $mode;
            $this->cropToHeight = false;
			$this->valid = true;
		}
		else if($mode == 'partners')
		{
			$this->filename = 'partners';
			$this->filewidth = 600;
			$this->fileheight = 150;
            $this->thmbwidth = 0;
            $this->thmbheight = 0;
			$this->stringdim = '600x150px';
			$this->selectQ = "select * from ". PARTNERS_TABLE . ' ORDER BY p_title ASC';
			$this->existQ = "select * from ". PARTNERS_TABLE . " Where p_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". PARTNERS_TABLE . " Set p_image = '' Where p_id = " . $_POST['itemID'];
			$this->updateQ = "update ". PARTNERS_TABLE . " Set p_image = '{logourl}' Where p_id = " . $_POST['itemID'];
			$this->title_field = 'p_title';
			$this->image_field = 'p_image';
			$this->image_id = 'p_id';
			$this->form_id = 'p_id';
			$this->action = 'partners';
			$this->mode = $mode;
            $this->cropToHeight = false;
			$this->valid = true;
		}
		
		if($mode == 'champimage')
		{
			$this->filename = 'champentry';
			$this->filewidth = 640;
			$this->fileheight = 480;
            $this->thmbwidth = 100;
            $this->thmbheight = 75;
			$this->stringdim = '640x480px';
			$this->selectQ = "select cs_image, c_id, c_title, CONCAT(cs_champ, '_', cs_driver) AS logo_id from ". CHAMPSIGNUPS_TABLE . ", " . 
									CHAMPIONSHIPS_TABLE . " Where cs_champ = c_id AND  cs_champ = " . $id;
			$this->existQ = "select cs_image, c_id, c_title, CONCAT(cs_champ, '_', cs_driver) AS logo_id from ". CHAMPSIGNUPS_TABLE . ", " . 
									CHAMPIONSHIPS_TABLE . " Where cs_champ = c_id AND cs_driver = " . $_SESSION['valid_id'] . " AND cs_champ = " . $id;
			$this->removeQ =  "update ". CHAMPSIGNUPS_TABLE . " Set cs_image = '' Where cs_imageThmb = " . $id;
			$this->updateQ = "update ". CHAMPSIGNUPS_TABLE . " Set cs_image = '{logourl}', cs_imageThmb = '{thmburl}' Where cs_driver = " . 
								$_SESSION['valid_id'] . " AND cs_champ = " . $id;
			$this->title = $lang['ChampSignupImage'];
			$this->title_field = 'c_title';
			$this->image_field = 'cs_image';
			$this->image_id = 'logo_id';
			$this->form_id = 'c_id';
			$this->itemName = 'champid';
			$this->action = 'usercp.php?action=mychampionships';
			$this->mode = $mode;
			$this->action = 'champimage';
            $this->cropToHeight = true;
			$this->valid = true;
			
		}

		if($mode == 'licenses')
		{
			$this->filename = 'license';
			$this->filewidth = 300;
			$this->fileheight = 150;
            $this->thmbwidth = 120;
            $this->thmbheight = 60;
			$this->stringdim = '300x150px';
			$this->selectQ = "select * from ". LICENSES_TABLE . ' ORDER BY l_order ASC';
			$this->existQ = "select * from ". LICENSES_TABLE . " Where l_id = " . $_POST['itemID'];
			$this->removeQ =  "update ". LICENSES_TABLE . " Set l_image = '', l_imageThmb = '' Where l_id = " . $_POST['itemID'];
			$this->updateQ = "update ". LICENSES_TABLE . " Set l_image = '{logourl}', l_imageThmb = '{thmburl}' Where l_id = " . $_POST['itemID'];
			$this->title_field = 'l_name';
			$this->image_field = 'l_image';
			$this->image_id = 'l_id';
			$this->form_id = 'l_id';
			$this->action = 'licenses';
			$this->mode = $mode;
            $this->cropToHeight = false;
			$this->valid = true;
			
		}

		if($this->valid)
		{
			if(isset($_POST['uploadLogo']) && isset($_POST['itemID']))
			{
				$this->doUpload();
			}
			else if(isset($_POST['removeLogo']) && isset($_POST['itemID']))
			{
				$this->doRemove();
			}
			$this->setupEdit();
		}
	}

	function render()
	{
		if($this->valid)
		{		
			include_once('includes/addMessage.php');
			
			if($this->mess != '')
			{
				$this->mess->render();
			}
			
			foreach( $this->toRender as $key => $value){
				$value->render();
			}
		}			
	}
	
	function doRemove()
	{
		
		global $db, $lang;
		
		//get the record
		$result = $db->sql_query($this->existQ);
		if($row = $db->sql_fetchrow($result)) 
		{
			$result = $db->sql_query($this->removeQ);
			$this->mess = new addMessage(str_replace('{entry}', $row[$this->title_field], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doUpload()
	{
		global $db, $lang;
		
		$result = $db->sql_query($this->existQ);
		if($row = $db->sql_fetchrow($result)) 
		{		
			  $image = 0;
			  if( ($_FILES['uploadedfile']['type']=="image/gif") ) {
				 $image = imagecreatefromgif($_FILES['uploadedfile']['tmp_name']);
			  } 
			  elseif($_FILES['uploadedfile']['type']=="image/jpeg" || $_FILES['uploadedfile']['type']=="image/pjpeg" ) {
				 $image = imagecreatefromjpeg($_FILES['uploadedfile']['tmp_name']);
			  } 
			  elseif( $_FILES['uploadedfile']['type']=="image/png") {
				 $image = imagecreatefrompng($_FILES['uploadedfile']['tmp_name']);
			  }
			  else
			  {
				 $this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle']);
			  }
			  
			  if($image != 0)
			  {
				  //find out how many screenshots we already have	 
				  $screens = $this->filename . $row[$this->image_id];
				  
				  //target file name
				  $imageurl = "images/" . $screens . ".jpg"; 
				  
				  //resize and crop our image
				  $image = cropImage($this->filewidth, $this->fileheight, $image, $this->cropToHeight);
								  
				if(imagejpeg($image,$imageurl,95) )
				{
					$valid = false;
					$thmburl = '';
					$thmbimage = '';
					if($this->thmbwidth > 0)
					{
						$thmbimage = cropImage($this->thmbwidth, $this->thmbheight, $image, $this->cropToHeight);
						$thmburl = "images/" . $screens . "_thmb.jpg"; 
						if(imagejpeg($thmbimage,$thmburl,90) )
						{
							$valid = true;
						}
					}
					else
					{
						//don't need a thmb
						$valid = true;
					}
					//get the record
					$result = $db->sql_query($this->existQ);
					if($valid && $row = $db->sql_fetchrow($result)) 
					{
						$result = $db->sql_query(str_replace('{thmburl}', $thmburl,
											str_replace('{logourl}', $imageurl,$this->updateQ)));
						$this->mess = new addMessage(str_replace('{entry}', $row[$this->title_field], 
											$lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
					} 
					else
					{
						$this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle'].'1');
					}
				}
				else
				{
					$this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle'].'2');
				}
			} 
			else 
			{ 
				 $this->mess = new addMessage($lang['LogoError'], $lang['LogoErrorTitle'].'3');
			}
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle'].'4');
		}
	}
		
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_logo.php');
		
		//get the record
		
		$result = $db->sql_query($this->selectQ);
		$introMess = false;
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$introMess = true;
			}
			
			$title = $this->title == '' ? $row[$this->title_field] : $this->title;
			$this->toRender[] = new edit_logo($this->action, $row[$this->form_id], $title, $row[$this->image_field], $this->stringdim, $this->itemName);
		}
	}
}
?>